from flask import *
from io import BytesIO
import qrcode, base64

__version__ = '0.0.1'

app = Flask(__name__)
app.secret_key = 'C-i6tyW8~gmckBS'
app.config.update(DEBUG=True)

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/api/qrcode', methods=['GET', 'POST'])
def QRcode():
    if request.method == 'POST':
        data = str(request.form.get('data', 'None'))
    else:
        data = str(request.args.get('data', 'None'))
    
    if data == '':data = 'None'

    qr = qrcode.QRCode(
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1,
    )
    data = data.encode('utf-8')
    qr.add_data(data)
    img = qr.make(fit=True)
    out = BytesIO()
    img = qr.make_image(fill_color="black", back_color="white")
    img.save(out, 'PNG')
    return u"data:image/png;base64," + base64.b64encode(out.getvalue()).decode('ascii')

@app.route('/app/<api>')
def use(api="code"):
    if api == "code":
        return render_template('api/code.html')
    return "mo"

@app.route('/alive')
def alive():
    return 'window.location.replace("http://127.0.0.1/app/code");'

@app.route('/cloud', methods=['GET', 'POST'])
def cloud(more=None):
    if request.method == 'GET':
        return render_template('api/cloud.html')
    else:

        return 'OK'

@app.route('/associate')
def associate():
    return render_template('api/associate.html')

@app.route('/chat')
def chat():
    return render_template('api/chat.html')

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=80)